﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Ah.Testudin.Engine.Actors;
using Ah.Testudin.Engine.Actors.Components;
using Ah.Testudin.Engine.Events;
using Ah.Testudin.Engine.Events.Components;

namespace Ah.Testudin.Engine.Processes
{
    public class PhysicsUpdateProcess : ProcessBase
    {
        #region Private Member Variables

        private IList<PhysicsComponent> _physicsComponents;

        #endregion

        #region Constructors

        public PhysicsUpdateProcess(IEventManager eventManager)
        {
            if (eventManager == null)
            {
                throw new ArgumentNullException("eventManager");
            }

            _physicsComponents = new List<PhysicsComponent>();

            eventManager.RegisterEventHandler(
                EventType.PhysicsComponentCreated,
                HandlePhysicsComponentCreated);
        }

        #endregion

        #region ProcessBase

        public override void Update(GameTime gameTime)
        {
            foreach (PhysicsComponent currentComponent in _physicsComponents)
            {
                currentComponent.Update(gameTime);
            }
        }

        #endregion

        #region Private Methods

        private void HandlePhysicsComponentCreated(ITestudinEvent testudinEvent)
        {
            PhysicsComponentCreatedEvent componentCreatedEvent = testudinEvent as PhysicsComponentCreatedEvent;

            if (componentCreatedEvent == null)
            {
                throw new ArgumentException("Expected PhysicsComponentCreatedEvent");
            }

            _physicsComponents.Add(componentCreatedEvent.PhysicsComponent);
        }

        #endregion
    }
}
